<?php
namespace app\servershoppingcart\logic;
use \app\manage\logic\BaseLogic;
use \think\Db;
use \app\manage\service\ApiBase;

class Shoppingcart extends BaseLogic{
	
	protected $name = 'shoppingcart';
	protected $config = 'db_config_shoppingcart';
	protected $shoppingcartModel = 'app\servershoppingcart\model\Shoppingcart';

	//添加购物车
	public function addCart($data): array 
	{
		//判断购物车数量是否超过规定最大值  MAX_CART_NUMBER
		$_where['user_id'] = $data['user_id'];
		$cartNumber = $this->model($this->shoppingcartModel)->where($_where)->count();
		if($cartNumber > MAX_CART_NUMBER){
			return verify_err(null, '购物车数量已超过' . MAX_CART_NUMBER . '请清理购物车后再添加');
		}
		$isExit = $this->_isExit($data);
		if($isExit['status'] == SUCCESS_STATUS){  //更新购物车该商品数量
			$res = $this->model($this->shoppingcartModel)->where($isExit['res'])->setInc('number', $data['number']);
		}else{  //添加到购物车
			$data = $this->_regroupCartInfo($data);
			if($data['price'] != $data['final_price']){
				return verify_err(null, '商品最终人民币单价传入错误');
			}
			if($data['orther_price']['currency'] != $data['final_currency_price']){
				return verify_err(null, '商品最终亚数币单价传入错误');
			}
			$res = $this->model($this->shoppingcartModel)->allowField('user_id,goods_id,goods_info,price,orther_price,number,create_time,update_time')->save($data);
		}
		return auto_return($res, '成功添加购物车', '添加购物车失败');
	}

	/*判断该商品是否存在  若存在,返回存在的$_where语句*/
	protected function _isExit($data): array
	{
		//获取多选的商品属性en_name数组
		foreach ($data['goods_info']['goods_attrs'] as $k => $v) {
			foreach($v as $kk => $vv){
				if(isset($vv['attr_type']) && $vv['attr_type'] == 4){
					$fieldName = 'goods_info$.choose.' . $vv['en_name'] . '.value';
					$_where[$fieldName] = $data['goods_attrs'][$vv['en_name']]['value'];
				}
			}
		}
		$_where['user_id'] = $data['user_id'];
		$_where['goods_id'] = $data['goods_info']['id'];
		$info = $this->model($this->shoppingcartModel)->where($_where)->find();
		return $info ? auto_success($_where, '存在属性相同的该商品') : auto_failure(null, '不存在属性相同的该商品');
	}

	/*加入购物车 - 重新组建购物车信息 商品信息追加字段choose*/
    protected function _regroupCartInfo(array $data): array 
    {
    	$data['goods_id'] = $data['goods_info']['id'];
    	$choose = [];
    	$final_price = $data['goods_info']['price'];
    	$final_currency_price = $data['goods_info']['orther_price']['currency'];
    	foreach ($data['goods_attrs'] as $k => $v) {
    		$choose[$v['en_name']] = $v;
    		isset($v['change_price']) && $final_price = bcadd($final_price, $v['change_price']);
    		isset($v['change_currency_price']) && $final_currency_price = bcadd($final_currency_price, $v['change_currency_price'], 4);
    	}
    	$data['price'] = $final_price;
    	$data['orther_price']['currency'] = $final_currency_price;
    	$data['goods_info']['choose'] = $choose;
    	return $data;
    }

	/*从购物车删除某商品*/
	public function delCartGoods($data): array 
	{
		$_where['user_id'] = $data['user_id'];
		$_where['id'] = $data['id'];
		$res = $this->model($this->shoppingcartModel)->where($_where)->delete();
		return auto_return($res, '成功移除购物车', '移除购物车失败');
	}

	public function delCartByIds($data): array 
	{
		$_where['user_id'] = $data['user_id'];
		$_where['id'] = ['in', $data['ids']];
		$res = $this->model($this->shoppingcartModel)->where($_where)->delete();
		return auto_return($res, '成功移除购物车', '移除购物车失败');
	}

	/*获取我的购物车*/
	public function getMyCart($data): array 
	{
		$_where['user_id'] = $data['user_id'];
		$res = $this->model($this->shoppingcartModel)
                ->where($_where)
                ->order('id DESC')
                ->select();
        return auto_return($res);
	}

	/*购物车-1*/
	public function decMyCart($data): array 
	{
		$_where['user_id'] = $data['user_id'];
		$_where['id'] = $data['id'];
		$cartInfo = $this->model($this->shoppingcartModel)->where($_where)->find();
		if($cartInfo['number'] == 1){
			return verify_err(null, '该商品数量只有一件了');
		}
		$res = $this->model($this->shoppingcartModel)->where($_where)->setDec('number');
		return auto_return($res);
	}

	/*艺货 获取我的购物车中的商品单价变动信息*/
	public function goodsChangePriceInfo($data): array 
	{
		$infoList = [];
		//改变最新商品信息的索引键为goods_id
		$newGoodsInfos = reset_array_key($data['goods_infos'], 'id');
		//以购物车为基准
		foreach ($data['shoppingcart'] as $k => $v) {
			$difference = bcsub($newGoodsInfos[$v['goods_id']]['price'], $v['goods_info']['price']);
			$currency_difference = bcsub($newGoodsInfos[$v['goods_id']]['orther_price']['currency'], $v['goods_info']['orther_price']['currency']);
			if($difference || $currency_difference){ //价格有变动
				//以id为准，为了防止这种情况： 商品id=100的商品加入购物车后,商品改价。然后再加入到购物车，不改价
				$addChange['id'] = $v['id'];    
 				$addChange['goods_id'] = $v['goods_id'];
 				$addChange['title'] = $v['goods_info']['title'];
 				$addChange['change_price'] = $difference;
 				$addChange['change_currency_price'] = $currency_difference;
 				$addChange['cart_price'] = $v['price'];
 				$addChange['cart_currency_price'] = $v['orther_price']['currency'];
 				$addChange['now_price'] = bcadd($v['price'], $difference);
 				$addChange['now_currency_price'] = bcadd($v['orther_price']['currency'], $currency_difference, 4);
 				$msg = $addChange['title'];
 				$appendMsg = '，当前价格发生变化，现价为：人民币价格：￥' .  bcdiv($addChange['now_price'], 100, 2) . '元,亚数币价格：' . $addChange['now_currency_price'] . '个';
 				$msg .= $appendMsg;
 				$addChange['msg'] = $msg;
 				$infoList[] = $addChange;
			}
		}
		//重组结果
		if($infoList){
			$res['number'] = count($infoList);
			$res['list'] = $infoList;
		}else{
			$res['number'] = 0;
		}
		return auto_return($res);
	}
}

